﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();
        
        try
        {
            if (!IsPostBack)
            {
                GenerateDates gd = new GenerateDates();

                // Lấy toàn bộ năm đổ vào ddlYear
                ddlYear.DataSource = gd.generateYear();
                ddlYear.DataTextField = "Text";
                ddlYear.DataValueField = "Value";
                ddlYear.DataBind();                
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
            GenerateDates gd = new GenerateDates();

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            // Lấy toàn bộ năm đổ vào ddlDay
            ddlDay.DataSource = gd.generateDay(year, month);
            ddlDay.DataTextField = "Text";
            ddlDay.DataValueField = "Value";
            ddlDay.DataBind(); 
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
            GenerateDates gd = new GenerateDates();

            // Lấy 12 tháng đổ vào ddlMonth
            ddlMonth.DataSource = gd.generateMonth();
            ddlMonth.DataTextField = "Text";
            ddlMonth.DataValueField = "Value";
            ddlMonth.DataBind();

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            // Lấy toàn bộ năm đổ vào ddlDay
            ddlDay.DataSource = gd.generateDay(year, month);
            ddlDay.DataTextField = "Text";
            ddlDay.DataValueField = "Value";
            ddlDay.DataBind(); 
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 15)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtStreetAdress.Text = "";
        txtState.Text = "";
        txtCity.Text = "";
        txtPhoneNumber.Text = "";
        txtEmail.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
            string firstName = txtFirstName.Text;
            string lastName = txtLastName.Text;
            string street = txtStreetAdress.Text;
            string state = txtState.Text;
            string city = txtCity.Text;
            string phoneNumber = txtPhoneNumber.Text;
            string email = txtEmail.Text;
            string filePath = "";
            int gender = 0;

            if (rdbFemale.Checked)
            {
                gender = 1;
            }

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int day = Convert.ToInt32(ddlDay.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

            DateTime dateOfBirth = new DateTime(year, month, day);
            if (txtPhoneNumber.Text.Length <= 15 && udm.checkEmail(udm.createConnection(), email))
            {
                if (fuImage.HasFile)
                {
                    CheckValid cv = new CheckValid();

                    if (cv.CheckFileType(fuImage.FileName))
                    {
                        filePath = "~/Image/" + fuImage.FileName;

                        if (System.IO.File.Exists(MapPath(filePath)))
                        {
                            int counter = 2;

                            string tempFileName = "";

                            while (System.IO.File.Exists(MapPath(filePath)))
                            {
                                tempFileName = counter.ToString() + fuImage.FileName;

                                filePath = "~/Image/" + tempFileName;

                                counter++;
                            }
                        }

                        fuImage.SaveAs(MapPath(filePath));
                    }
                }     

                if (!udm.addNewStudent(udm.createConnection(), firstName, lastName, gender, dateOfBirth, street, state, city, phoneNumber, email, filePath))
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
                else
                {
                    string id = udm.getIDOfAddmissionByEmail(udm.createConnection(), email);
                    string path = "RegistSuccess.aspx?ID=" + id;
                    Response.Redirect(path);
                }
            }
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string email = txtEmail.Text;

        if (udm.checkEmail(udm.createConnection(), email))
            args.IsValid = true;
        else
            args.IsValid = false;
    }
}